﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DDE_Server.Object;
using DDE_Server.Presenter;

namespace DDE_Server.View
{
    public partial class DispatchView : UserControl
    {
        private PjmInstDispRate InstantDispRate = null;
        private DDEConfigs config = null;
        private DispatchPresenter presenter = null;
        public PjmInstDispRate InstDispRate 
        {
            set 
            {
                if (InvokeRequired)
                {
                    Action<PjmInstDispRate> callback = new Action<PjmInstDispRate>(setDispRate);
                    Invoke(callback,value);
                }
                else 
                {
                    if (value != null)
                    {
                        InstantDispRate = value;
                        DisplayItems();
                    }
                }
            }
            get {
                return InstantDispRate;
            }
        }

        public DispatchView()
            : this(DDEConfigs.Default)
        {
            
        }
        public DispatchPresenter Presenter {
            get {
                return presenter;
            }
        }

        public DispatchView( DDEConfigs config )
        {
            this.config = config;
            InitializeComponent();
            presenter = new DispatchPresenter(this, this.config);
        }

        //public DDEConfigs Config
        //{
        //    set
        //    {
        //        config = value;
        //    }
        //    get
        //    {
        //        return config;
        //    }

        //}

        public void setDispRate(PjmInstDispRate instantDispRate)
        {
            InstDispRate = instantDispRate;
        }

        void DisplayItems()
        {
            rate_listBox.Items.Clear();
            dispatch_Label.Text = InstantDispRate.LastUpdate.ToString();

            for (int i = 0; i < InstantDispRate.RateTable.Count; i++)
            {
                //if (p > 0 && p < 40)
                //{
                //    Console.BackgroundColor =
                //        ConsoleColor.DarkGreen;
                //    Console.ForegroundColor = ConsoleColor.White;

                //}
                //else if (p >= 40 && p < 70)
                //{
                //    Console.BackgroundColor = ConsoleColor.Green;
                //    Console.ForegroundColor = ConsoleColor.Black;

                //}

                //else if (p >= 70 && p < 100)
                //{
                //    Console.BackgroundColor = ConsoleColor.Yellow;
                //    Console.ForegroundColor = ConsoleColor.Black;

                //}
                //else if (p >= 100 && p < 200)
                //{
                //    Console.BackgroundColor = ConsoleColor.DarkYellow;
                //    Console.ForegroundColor = ConsoleColor.Black;

                //}
                //else if (p >= 200)
                //{
                //    Console.BackgroundColor = ConsoleColor.Red;
                //    Console.ForegroundColor = ConsoleColor.Black;

                //}
                //else
                //{
                //    Console.BackgroundColor = ConsoleColor.Black;
                //}
                rate_listBox.Items.Add(String.Format("{0:00.00}", InstantDispRate.RateTable[i]));
            }
        }

        //private void dola_listBox_DrawItem(object sender, DrawItemEventArgs e)
        //{
        //    //string sOoutput = dola_listBox.Items[e.Index].ToString();
        //    //float folength = e.Graphics.MeasureString(sOoutput, e.Font).Width;
        //    //float fpos = (dola_listBox.Width - folength)/2;
        //    //SolidBrush brushBack = new SolidBrush(e.BackColor);
        //    //e.Graphics.FillRectangle(brushBack, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
        //    //SolidBrush brush = new SolidBrush(e.ForeColor);
        //    //e.Graphics.DrawString(sOoutput, e.Font, brush, fpos, e.Bounds.Top);
        //}

        
    }
}
